<template>
	<transition name="el-zoom-in-center">
		<div class="JNPF-preview-main">
			<div class="JNPF-common-page-header">
				<el-page-header @back="goBack" :content="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"/>
				<div class="options">
					<el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">确 定</el-button>
					<el-button @click="goBack">取 消</el-button>
				</div>
			</div>
			<el-row :gutter="15"  class="main" :style="{margin: '0 auto',width:'100%'}">
			<el-form ref="elForm" :model="dataForm" size="medium" label-width="100px" label-position="right" :disabled="!!isDetail" :rules="rules">
				<el-col :span="12">
					<el-form-item label="事件名称" prop="eventName" required >
						<el-input v-model="dataForm.eventName" placeholder="请输入" clearable :style='{"width":"100%"}' prefix-icon="" suffix-icon="" >
							<template slot="prepend"></template>
							<template slot="append"></template>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="所属单位" prop="companyId" required >
						<el-select v-model="dataForm.companyId" placeholder="请选择" clearable :style='{"width":"100%"}' filterable >
							<el-option v-for="(item, index) in companyIdOptions" :key="index" :label="item.F_FullName" :value="item.F_Id" ></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="报警类型" prop="facility" required >
						<el-select v-model="dataForm.facility" placeholder="请选择" clearable :style='{"width":"100%"}' filterable >
							<el-option v-for="(item, index) in facilityOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="处理状态" prop="status" required >
						<el-select v-model="dataForm.status" placeholder="请选择" clearable :style='{"width":"100%"}' filterable >
							<el-option v-for="(item, index) in statusOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="图片" prop="images" >
						<JNPF-UploadImg v-model="imagesList" accept="" :fileSize="20" sizeUnit="MB" :limit="9" >
						</JNPF-UploadImg>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="处置人员" prop="disposePeople" required >
						<el-select v-model="dataForm.disposePeople" placeholder="请选择" clearable disabled :style='{"width":"100%"}' filterable >
							<el-option v-for="(item, index) in disposePeopleOptions" :key="index" :label="item.F_RealName" :value="item.F_Id" ></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12" style="display:none;">
					<el-form-item label="处置时间" prop="disposeTime" required >
						<el-date-picker v-model="dataForm.disposeTime" placeholder="请选择" clearable :style='{"width":"100%"}' type="datetime" format="yyyy-MM-dd HH:mm:ss" value-format="timestamp" >
						</el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="事件描述" prop="eventDescribe" required >
						<el-input v-model="dataForm.eventDescribe" placeholder="请输入" show-word-limit :style='{"width":"100%"}' type="textarea" :autosize='{"minRows":4,"maxRows":100}' >
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="处置结果" prop="disposeResult" required >
						<el-input v-model="dataForm.disposeResult" placeholder="请输入" show-word-limit :style='{"width":"100%"}' type="textarea" :autosize='{"minRows":4,"maxRows":100}' >
						</el-input>
					</el-form-item>
				</el-col>
			</el-form>
			</el-row>
		</div>
	</transition>

</template>
<script>
import request from '@/utils/request'
import { mapState, mapGetters } from "vuex";
import {getDictionaryDataSelector}from '@/api/systemData/dictionary'
import {previewDataInterface}from '@/api/systemData/dataInterface'
export default {
	computed:{
		...mapGetters(['userInfo'])
	},
    data() {
      return {
        visible: false,
        isDetail: false,
        dataForm: {
					eventName:undefined,
					companyId:undefined,
					facility:undefined,
					status:undefined,
					images:[],
					disposePeople:undefined,
					disposeTime:undefined,
					eventDescribe:undefined,
					disposeResult:undefined,

        },
        rules: {
						eventName:[
						{
							required:true,
							message:'请输入事件名称',
							trigger:'blur'
						},
						],
						companyId:[
						{
							required:true,
							message:'请输入所属单位',
							trigger:'blur'
						},
						],
						facility:[
						{
							required:true,
							message:'请输入报警类型',
							trigger:'blur'
						},
						],
						status:[
						{
							required:true,
							message:'请输入处理状态',
							trigger:'blur'
						},
						],
						disposePeople:[
						{
							required:true,
							message:'请输入处置人员',
							trigger:'blur'
						},
						],
						disposeTime:[
						{
							required:true,
							message:'请输入处置时间',
							trigger:'blur'
						},
						],
						eventDescribe:[
						{
							required:true,
							message:'请输入事件描述',
							trigger:'blur'
						},
						],
						disposeResult:[
						{
							required:true,
							message:'请输入处置结果',
							trigger:'blur'
						},
						],

        },
				companyIdOptions:[],
				facilityOptions:[{"fullName":"移动端上报","id":"1"},{"fullName":"风险活动预警","id":"2"},{"fullName":"风险变更预警","id":"3"}],
				statusOptions:[{"fullName":"报警","id":"0"},{"fullName":"已确认","id":"1"},{"fullName":"已忽略","id":"2"},{"fullName":"已处理","id":"3"}],
				disposePeopleOptions:[],

					imagesList:[],

      }
    },
    created() {
		this.getcompanyIdOptions();
		this.getdisposePeopleOptions();
    },
    methods: {
		getcompanyIdOptions()
		{
			previewDataInterface('772d2cbcadd24df79243e3d92ef5f690').then(res => {
				this.companyIdOptions = res.data
			})
		},
		getdisposePeopleOptions()
		{
			previewDataInterface('8bca424f16d6448ab09e57a028853aa9').then(res => {
				this.disposePeopleOptions = res.data;
				this.dataForm.disposePeople = this.userInfo.userId;
			})
		},

           goBack() {
                    this.$emit('refresh')
                },
      init(id, isDetail) {

        this.dataForm.id = id || 0;
        this.visible = true;
        this.isDetail = isDetail || false;
        this.$nextTick(() => {
          this.$refs['elForm'].resetFields();
          if (this.dataForm.id) {
            request({
              url: '/api/System/swWarningEventManageC/' + this.dataForm.id,
              method: 'get'
            }).then(res =>{
              this.dataForm = res.data;
				this.imagesList = this.dataForm.images ? JSON.parse(this.dataForm.images) : [];

            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
		  let timestamp = Date.parse(new Date());
		  this.dataForm.disposeTime = timestamp;
				this.$set(this.dataForm, 'images', JSON.stringify(this.imagesList));

        this.$refs['elForm'].validate((valid) => {
          if (valid) {
            if (!this.dataForm.id) {
              request({
                url: '/api/System/swWarningEventManageC',
                method: 'post',
                data: this.dataForm,
              }).then((res) =>{
                this.$message({
                  message: res.msg,
                  type: 'success',
                  duration: 1000,
                  onClose: () =>{
                    this.visible = false;
                    this.$emit('refresh', true);
                  }
                })
              })
            } else {
              request({
                url: '/api/System/swWarningEventManageC/' + this.dataForm.id,
                method: 'PUT',
                data: this.dataForm
              }).then((res) =>{
                this.$message({
                  message: res.msg,
                  type: 'success',
                  duration: 1000,
                  onClose: () =>{
                    this.visible = false;
                    this.$emit('refresh', true);
                  }
                })
              })
            }
          }
        })
      },

    }
  }
  </script>